package com.michaelbulava.TFS.ui.config;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.persistence.IdeaPersistenceStoreProvider;
import com.michaelbulava.TFS.ui.dialogs.connect.CredentialsCompleteDialog;
import com.michaelbulava.TFS.ui.dialogs.connect.CredentialsDialog;
import com.microsoft.tfs.core.credentials.CachedCredentials;
import com.microsoft.tfs.core.credentials.CredentialsManager;
import com.microsoft.tfs.core.credentials.CredentialsManagerFactory;
import com.microsoft.tfs.core.httpclient.Credentials;
import com.microsoft.tfs.core.ws.runtime.exceptions.FederatedAuthException;
import com.microsoft.tfs.core.ws.runtime.exceptions.TransportAuthException;
import com.microsoft.tfs.util.Check;

import java.net.URI;
import java.text.MessageFormat;

/**
 * Created by mbulava on 5/6/2015.
 */
public class UITransportUsernamePasswordAuthRunnable extends UITransportAuthRunnable {
    private final URI serverURI;
    private final Credentials credentials;
    private final TransportAuthException exception;

    public UITransportUsernamePasswordAuthRunnable(URI serverURI, Credentials credentials) {
        this(serverURI, credentials, null);
    }

    public UITransportUsernamePasswordAuthRunnable(URI serverURI, Credentials credentials, TransportAuthException exception) {
        Check.notNull(serverURI, "serverURI");
        Check.notNull(credentials, "credentials");

        this.serverURI = serverURI;
        this.credentials = credentials;
        this.exception = exception;
    }

    @Override
    protected CredentialsCompleteDialog getCredentialsDialog() {
        final CredentialsManager credentialsManager = CredentialsManagerFactory.getCredentialsManager(IdeaPersistenceStoreProvider.INSTANCE);

        boolean needFedAuth = (exception instanceof FederatedAuthException);
        final CredentialsDialog credentialsDialog = new CredentialsDialog((Project)null, this.serverURI, needFedAuth);

        //TODO make sure that Preemptive U/P will work otherwise we will need to setup Alt Credentials at TFS online.
        if (this.exception != null && !needFedAuth)
            credentialsDialog.setErrorMessage(exception.getLocalizedMessage());
        else if (needFedAuth){
            String error = MessageFormat.format(Messages.getString("VisualStudioOnline.FedAuthRequiredMessage_Format"),
                    new Object[] {
                            serverURI,
                            Messages.getString("VisualStudioOnline.UserSetupLink")
                    });
            credentialsDialog.setErrorMessage(error);
        }
            //credentialsDialog.setErrorMessage(exception.getLocalizedMessage());

        if (credentialsManager.canWrite()){
            credentialsDialog.setAllowSavePassword(true);

            credentialsDialog.addCredentialsCompleteListener(new CredentialsCompleteDialog.CredentialsCompleteListener() {
                @Override
                public void credentialsComplete() {
                    if (credentialsDialog.getExitCode() == DialogWrapper.OK_EXIT_CODE && credentialsDialog.isSavePasswordChecked()){
                        credentialsManager.setCredentials(new CachedCredentials(serverURI, credentials));
                    }
                }
            });
        }
        return credentialsDialog;
    }
}
